clear all
set more off

cd "ADD HERE FOLDER IN WHICH DATA IS CONTAINED"


********************************************************************************
	cap file close output
	
	
	#delimit;
	file open output using "output.tex", write text replace;
	
	local stats N mean se p50 p75 p90 p95 p99;
	local stats_num = wordcount("`stats'");
	
	file write output "\begin{table}[htp]" _n
					  "\centering" _n
					  "\begin{tabularx}{\linewidth}{X*{1}ccccccccc}" _n
			          "\toprule" _n
_tab _tab _tab _tab	  "&" _tab    
				      "&" _tab "Number of "   "&"_tab  "Mean " 
					  "&" _tab "Std "         "&"_tab  "50th" "&"_tab   "75th" 
					  "&" _tab "90th "        "&"_tab  "95th" "&"_tab   "99th" 
					  "\\" _n
_tab _tab _tab _tab	  "&" _tab 
                      "&" _tab  "Observations" "&" _tab         
					  "&" _tab  "Dev"          "&" _tab  "Pctl" "&"_tab  "Pctl" 
					  "&" _tab  "Pctl"         "&" _tab   "Pctl" "&"_tab  "Pctl" 
					  "\\" _n
					  "\toprule" _n
					  "\addlinespace[1em]" _n;
	
	/*Panel a*/	
	file write output "\multicolumn{`stats_num'}{l}{\textbf{Panel A: Patent-level Data}}\\" _n
					  "\addlinespace[0.25em]" _n;
	file write output "\cmidrule(lr){1-`=`stats_num'+2'}" _n;	
	
	file write output "\addlinespace[0.5em]" _n;
    file write output "\textit{\textbf{Patents filed in 1910–1929}}& &\\" _n
					  "\addlinespace[0.5em]" _n;

	use "input\final_patent_list_rep.dta", clear;
		gen pre=1 if fyear>=1910 & fyear<=1929;
	gen post=1 if fyear>=1930 & fyear<=1949;

	sum cit_received if pre==1 & patent_category=="Independent",d;
	
	file write output %30s "Independent Number Citations & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;
	
	sum cit_received if pre==1 & patent_category=="US Firm",d;
	
	file write output %30s "Firm Number Citations & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;
		
	file write output "\addlinespace[0.5em]" _n;
	
	file write output "\textit{\textbf{Patents filed in 1930–1949}}& &\\" _n;
	file write output "\addlinespace[0.5em]" _n;
	sum cit_received if post==1 & patent_category=="Independent",d;
	
	file write output %30s "Independent Number Citations & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;
	
	sum cit_received if post==1 & patent_category=="US Firm",d;
	
	file write output %30s "Firm Number Citations & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;
	
	file write output "\toprule" _n "\addlinespace[1em]" _n;
	
	/* Panel B */
	
	file write output "\multicolumn{`stats_num'}{l}"
	                  "{\textbf{Panel B:  County-decade-level Data}}" _n 
	                  "& &\\" _n
					  "\addlinespace[0.25em] \cmidrule(lr){1-`=`stats_num'+2'}" _n
					  "\addlinespace[0.5em]" _n;
	
	use "input\final_data_county_rep.dta", clear;
	gen pre=1 if fdecade==1910 | fdecade==1920;
	gen post=1 if fdecade==1930 | fdecade==1940;

	file write output "\textit{\textbf{Patents filed in 1910–1929}}\\" _n;
	file write output "\addlinespace[0.5em]" _n;
	sum ind_numpat if pre==1,d;
	
	file write output %30s "Independent Number Patents & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
					  
	sum usfirm_numpat if pre==1,d;
	
	file write output %30s "Firm Number Patents & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;		
	sum ind_numctd if pre==1,d;
	
	file write output %30s "Independent Number Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
		
	sum usfirm_numctd if pre==1,d;
	
	file write output %30s "Firm Number Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
		
	sum ind_avecit if pre==1,d;
	file write output %30s "Independent Average Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
					  
	sum usfirm_avecit if pre==1,d;
	file write output %30s "Firm Average Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
					  
	file write output "[0.5em]" _n;			
	file write output "\textit{\textbf{Patents filed in 1930-1949}}\\" _n;
	file write output "\addlinespace[0.5em]" _n;
	sum ind_numpat if post==1,d;
	
	file write output %30s "Independent Number Patents & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;
	
	sum usfirm_numpat if post==1,d;
	
	file write output %30s "Firm Number Patents & &";
	
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;		
	
	
	sum ind_numctd if post==1,d;
	
	file write output %30s "Independent Number Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
		
	sum usfirm_numctd if post==1,d;
	
	file write output %30s "Firm Number Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
		
	sum ind_avecit if post==1,d;
	file write output %30s "Independent Average Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	
		
	sum usfirm_avecit if post==1,d;
	file write output %30s "Firm Average Citations & &";
	file write output %10.0fc  (r(N))     _tab "& "   
	                  %10.1fc  (r(mean))  _tab "&" 
				      %10.1fc  (r(sd))    _tab "&"
					  %10.1fc  (r(p50))   _tab "&"
					  %10.1fc  (r(p75))   _tab "&" 
					  %10.1fc  (r(p90))   _tab "&"
				      %10.1fc  (r(p95))    _tab "&"  
					  %10.1gc  (r(p99))   _tab "\\" _n;	

	file write output "\bottomrule"    _n
					  "\end{tabularx}" _n
	                  "\end{table}"    _n;
	file close output;	
	
	filefilter "output.tex" "output/table2.tex", from(".0") to("") replace; 
	erase "output.tex";				  
					  
					  